package com.zr.controller.driverclient;


import com.zr.service.driverclient.DriverService;
import com.zr.utils.AjaxResult;
import com.zr.utils.RedisUtil;
import com.zr.vo.base.BaseDriver;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/driver")
@CrossOrigin
public class DriverController {
    @Resource
    private DriverService driverService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 司机端登录方法
     *
     * @param baseDriver
     * @return
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody BaseDriver baseDriver) {
        BaseDriver loginBaseDriver = driverService.login(baseDriver);
        if (loginBaseDriver != null) {
            //设置返回的Map对象
            Map<String, Object> objectMap = new HashMap<>();
            objectMap.put("driver", loginBaseDriver);
            //自定义认证使用的token
            String token = UUID.randomUUID().toString();
            objectMap.put("token", token);
            //token存入redis并设置key过期时间
            redisUtil.set(token, loginBaseDriver, 15L, TimeUnit.MINUTES);

            return AjaxResult.success(200, "登录成功", objectMap);
        } else {
            return AjaxResult.fail(200, "账号或密码错误", null);
        }
    }

    /**
     * 修改司机信息
     *
     * @param baseDriver
     * @return
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody BaseDriver baseDriver) {
        int a = driverService.edit(baseDriver);
        if (a > 0) {
            return AjaxResult.success(200, "修改成功", null);
        } else {
            return AjaxResult.success(200, "修改失败", null);
        }

    }

    /**
     * 根据司机身份证查询司机信息
     *
     * @param driverId
     * @return
     */
    @PostMapping("/selectByDriverId/{driverId}")
    public AjaxResult selectByDriverId(@PathVariable("driverId") String driverId) {
        return AjaxResult.success(200, "查询成功", driverService.selectByDriverId(driverId));
    }

}
